package com.lh.glorypavilion.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lh.glorypavilion.service.edu.entity.Video;
import com.lh.glorypavilion.service.edu.feign.VodMediaService;
import com.lh.glorypavilion.service.edu.mapper.VideoMapper;
import com.lh.glorypavilion.service.edu.service.IVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author lhui
 * @since 2020-10-12
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {

    @Autowired
    private VodMediaService vodMediaService;

    @Override
    public void removeMediaVidemById(String id) {
        //根据videoId找到视频id
        Video video = baseMapper.selectById(id);
        String videoSourceId = video.getVideoSourceId();

        vodMediaService.removeVideo(videoSourceId);
    }

    @Override
    public void removeMediaVidemByChapterId(String id) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("video_source_id");
        queryWrapper.eq("chapter_id",id);

        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        List<String> videoSourseIdList = getVideoSourseIdList(maps);
        vodMediaService.removeList(videoSourseIdList);
    }

    @Override
    public void removeMediaVidemByCourseId(String id) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("video_source_id");
        queryWrapper.eq("course_id",id);
        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        List<String> videoSourseIdList = getVideoSourseIdList(maps);
        vodMediaService.removeList(videoSourseIdList);
    }

    public List<String> getVideoSourseIdList(List<Map<String, Object>> maps){
        List<String> stringList=new ArrayList<>();
        for (Map<String, Object> map : maps) {
            String video_source_id = (String) map.get("video_source_id");
            stringList.add(video_source_id);
        }
        return stringList;
    }
}
